package io.hirayclay.scrollingtextview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by hiray on 2017/11/27.
 *
 * @author hiray
 */

public class IndexScrollView extends View {

    private static final String BASE = "ABC啊阿一";
    private int mTextColor = 0xFFFFFFFF;
    private int mBaseItemTextColor = 0xFFFFFFFF;
    private int mTextSize = 15;
    private int mSpace = 80;
    private int mMaxItemCount = 5;
    private int mItemPadding = 20;
    TextPaint paint;
    private int itemHeight;
    private int itemRawHeight;

    private int mTotalOffset = 0;
    private float mY;

    public static final String[] strings = {"1", "2", "3", "4", "5", "6"};

    public IndexScrollView(Context context) {
        super(context);
    }

    public IndexScrollView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndexScrollView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.IndexScrollView);
        mTextColor = ta.getColor(R.styleable.IndexScrollView_index_text_color, mTextColor);
        mBaseItemTextColor = ta.getColor(R.styleable.IndexScrollView_base_item_text_color, mBaseItemTextColor);
        mTextSize = ta.getDimensionPixelSize(R.styleable.IndexScrollView_index_text_size, mTextSize);
        mSpace = ta.getDimensionPixelOffset(R.styleable.IndexScrollView_item_space, mSpace);
        mItemPadding = ta.getDimensionPixelSize(R.styleable.IndexScrollView_item_padding, mItemPadding);
        mMaxItemCount = ta.getInt(R.styleable.IndexScrollView_max_display_count, mMaxItemCount);
        ta.recycle();
        paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(mTextColor);
        paint.setTextSize(mTextSize);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Paint.FontMetrics fm = paint.getFontMetrics();
        itemRawHeight = (int) (fm.top - fm.bottom);
        itemHeight = (int) (fm.top - fm.bottom + mItemPadding * 2 + mSpace);
        mY = -fm.top+mItemPadding;
        int h = itemHeight * mMaxItemCount;
        int w = (int) (paint.measureText("1") + mItemPadding * 2 + getPaddingLeft() + getPaddingRight());
        super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY));
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int startIndex = mTotalOffset / itemHeight;
        int headerOffset = mTotalOffset % itemHeight;

        float yStart = mY - headerOffset;
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int width = getWidth();
        int a = 0;
        while (a < mMaxItemCount) {
            yStart += a * itemHeight;
            canvas.drawText(strings[a % mMaxItemCount], mItemPadding, yStart, paint);
            int rx = itemHeight / 10;
            canvas.drawPath(RoundedRect(paddingLeft, yStart - mY, width - paddingRight, yStart - mY + itemHeight, rx, rx, true, true, true, true), paint);
            a++;
        }

    }

    public void scrollToItem(int index, int duration) {

    }

    public Path RoundedRect(float left, float top, float right, float bottom, float rx, float ry,
                            boolean tl, boolean tr, boolean br, boolean bl) {
        Path path = new Path();
        if (rx < 0) {
            rx = 0;
        }
        if (ry < 0) {
            ry = 0;
        }
        float width = right - left;
        float height = bottom - top;
        if (rx > width / 2) {
            rx = width / 2;
        }
        if (ry > height / 2) {
            ry = height / 2;
        }
        float widthMinusCorners = (width - (2 * rx));
        float heightMinusCorners = (height - (2 * ry));

        path.moveTo(right, top + ry);
        if (tr)
            path.rQuadTo(0, -ry, -rx, -ry);//top-right corner
        else {
            path.rLineTo(0, -ry);
            path.rLineTo(-rx, 0);
        }
        path.rLineTo(-widthMinusCorners, 0);
        if (tl)
            path.rQuadTo(-rx, 0, -rx, ry); //top-left corner
        else {
            path.rLineTo(-rx, 0);
            path.rLineTo(0, ry);
        }
        path.rLineTo(0, heightMinusCorners);

        if (bl)
            path.rQuadTo(0, ry, rx, ry);//bottom-left corner
        else {
            path.rLineTo(0, ry);
            path.rLineTo(rx, 0);
        }

        path.rLineTo(widthMinusCorners, 0);
        if (br)
            path.rQuadTo(rx, 0, rx, -ry); //bottom-right corner
        else {
            path.rLineTo(rx, 0);
            path.rLineTo(0, -ry);
        }

        path.rLineTo(0, -heightMinusCorners);
        path.close();//Given close, last lineto can be removed.

        return path;
    }
}
